//
//  HistoryListViewController.m
//  YL8G
//
//  Created by Rocket on 12-8-22.
//
//

#import "HistoryListViewController.h"
#import "NoteInfo+YL8G.h"
#import "tyconsts.h"
#import "ModalAlert.h"
#import "UIColor+HTML.h"

@interface HistoryListViewController (Private)

- (void)createTitleView;
- (void)changeType:(int)type;
- (void)edit;
- (void)deleteAll;

@end

@implementation HistoryListViewController

@synthesize type=type_;

- (void)loadView
{
    [super loadView];
    [self createTitleView];
    
    UIButton* deleteAllBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    UIImage* image=[UIImage imageNamed:@"delete_all.png"];
    deleteAllBtn.frame=CGRectMake(2, kNavHeight-image.size.height, image.size.width+2, image.size.height);
    [deleteAllBtn addTarget:self action:@selector(deleteAll) forControlEvents:UIControlEventTouchUpInside];
    [deleteAllBtn setBackgroundImage:image forState:UIControlStateNormal];
    [deleteAllBtn setTitle:@"删除全部" forState:UIControlStateNormal];
    deleteAllBtn.titleLabel.font =[UIFont systemFontOfSize:13];
    UIBarButtonItem* leftButtonItem=[[UIBarButtonItem alloc] initWithCustomView:deleteAllBtn];
	[[self navigationItem] setLeftBarButtonItem:leftButtonItem];
	//[leftButtonItem release];
    
    UIBarButtonItem* rightButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(edit)];
	[[self navigationItem] setRightBarButtonItem:rightButtonItem];
	[rightButtonItem release];
}

- (void)viewDidLoad
{
    totalCount_=500;
    mainTable_.backgroundColor = [UIColor colorWithHTMLName:@"#F5F5F5"];
    [self requestListData:TRUE];
}

- (void)createTitleView
{
	NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"历史",@"收藏",nil];
	UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    segmentedControl.frame = CGRectMake(50, 5, 220, kNavHeight - 10);
	segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;//UISegmentedControlStyleBar;
	segmentedControl.selectedSegmentIndex = 0;
	segmentedControl.tintColor =  [UIColor orangeColor];
	[segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segmentedControl;
    [segmentedControl release];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestListData:TRUE];
}

- (void)addPullToRefreshHeader
{
    
}

//- (CGRect)getTableViewFrame
//{
//    return CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusHeight-kNavHeight);
//}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    
}

- (void)segmentedAction:(UISegmentedControl*)seg
{
    [self changeType:seg.selectedSegmentIndex];
}

- (void)changeType:(int)type
{
    if (type_!=type)
    {
        type_=type;
        [listFeeds_ removeAllObjects];
        listFeeds_=nil;
        [self requestListData:TRUE];
    }
}

- (BOOL)requestListData:(BOOL)refresh
{
    if (listFeeds_==nil || refresh)
    {
        NSArray* noteArray=nil;
        if (type_==0)
        {
            noteArray=[NoteInfo queryAllHistroy];
        }
        else
        {
            noteArray=[NoteInfo queryAllMarks];
        }
        [listFeeds_ release];
        listFeeds_ = [[NSMutableArray alloc] initWithCapacity:noteArray.count];
        [listFeeds_ addObjectsFromArray:noteArray];
        feeds_=listFeeds_;
    }
    [mainTable_ reloadData];
    return FALSE;
}

-(void)edit
{
	if(mainTable_.editing)
	{
		[mainTable_ setEditing:NO animated:YES];
		[self navigationItem].rightBarButtonItem.title=@"编辑";
	}
	else
	{
		[mainTable_ setEditing:YES animated:YES];
		[self navigationItem].rightBarButtonItem.title=@"完成";
	}
}

- (void)deleteAll
{
    if ([listFeeds_ count]>0)
    {
        NSUInteger answer = [ModalAlert ask:[NSString stringWithFormat:@"是否要删除所有%@记录?",type_==0?@"历史":@"收藏"]];
        if (answer)
        {
            BOOL ret=FALSE;
            if (type_==0)
            {
                ret=[NoteInfo deleteAllHistory];
            }
            else
            {
                ret=[NoteInfo deleteAllMarks];
            }
            
            [mainTable_ setEditing:NO animated:YES];
            [self navigationItem].rightBarButtonItem.title=@"编辑";
        }
    }
}

- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
	[self navigationItem].rightBarButtonItem.title=@"完成";
}

- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
	[self navigationItem].rightBarButtonItem.title=@"编辑";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
	//处理删除
	int row=[indexPath row];
	NoteInfo* item=[feeds_ objectAtIndex:row];
    BOOL ret=FALSE;
    if (type_==0)
    {
        ret=[item deleteHistoryRecordFromDatabase];
    }
    else
    {
        ret=[item deleteMarkRecordFromDatabase];
    }
	
	if(ret)
	{
		[listFeeds_ removeObjectAtIndex:row];
		
		[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
	}
	else
	{
		//删除失败
	}
    if ([listFeeds_ count]==0)
    {
		[mainTable_ setEditing:NO animated:YES];
		[self navigationItem].rightBarButtonItem.title=@"编辑";
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return @"删除";
}

@end
